/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.raitcon.hibernate.backbean;

import com.raitcon.hibernate.bean.Alumno;
import com.raitcon.hibernate.bean.Capacidad;
import com.raitcon.hibernate.bean.CedMatriTemp;
import com.raitcon.hibernate.bean.CedulaMatricula;
import com.raitcon.hibernate.bean.DetalleNota;
import com.raitcon.hibernate.bean.Nota;
import com.raitcon.hibernate.db.CedulaMatriculaDB;
import com.raitcon.hibernate.db.NotaDB;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import org.apache.log4j.Logger;

/**
 *
 * @author Mike
 */
@ManagedBean(name = "cedulaMatriculaBean")
@ViewScoped
public class CedulaMatriculaBean implements Serializable {
    
  
  protected static Logger log = Logger.getLogger(CedulaMatriculaBean.class); 
  private static  final  long serialVersionUID=-2377612760546575078L; 
  private static final int DECIMALS = 1;
  private static final int CLIENT_ROWS_IN_AJAX_MODE = 15;
  private static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_UP;
  private Integer idCedulaMatricula;
  private Alumno alumno;
  private List<CedMatriTemp> cedulaMatriculaList=new ArrayList<CedMatriTemp>(); 
  private Integer idGrado;
  private Integer idSeccion;
  private Integer idAsignatura ;
  private Integer idAnio;
  private Integer idEmpleado ;
  private int currentCedMatIndex;
  private CedMatriTemp editCedMatricula=new CedMatriTemp() ;
  private int page = 1;
  private int clientRows;
  private String mensaje;
  
  
  @PostConstruct
  public void init(){
       System.out.print("Running init LabelDetailJSF");
  }
  
  
  public void switchAjaxLoading(ValueChangeEvent event) {
        this.clientRows = (Boolean) event.getNewValue() ? CLIENT_ROWS_IN_AJAX_MODE : 0;
  
  }
  
  public void actualizar() {
        cedulaMatriculaList.set(currentCedMatIndex, editCedMatricula);
    }

    /**
     * @return the idCedulaMatricula
     */
    public Integer getIdCedulaMatricula() {
        return idCedulaMatricula;
        
    }

    /**
     * @param idCedulaMatricula the idCedulaMatricula to set
     */
    public void setIdCedulaMatricula(Integer idCedulaMatricula) {
        this.idCedulaMatricula = idCedulaMatricula;
    }

    /**
     * @return the alumno
     */
    public Alumno getAlumno() {
        return alumno;
    }

    /**
     * @param alumno the alumno to set
     */
    public void setAlumno(Alumno alumno) {
        this.alumno = alumno;
    }

    /**
     * @return the idGrado
     */
    public Integer getIdGrado() {
        return idGrado;
    }

    /**
     * @param idGrado the idGrado to set
     */
    public void setIdGrado(Integer idGrado) {
        this.idGrado = idGrado;
    }

    /**
     * @return the strIdSeccion
     */
    public Integer getIdSeccion() {
        return idSeccion;
    }

    /**
     * @param strIdSeccion the strIdSeccion to set
     */
    public void setIdSeccion(Integer idSeccion) {
        this.idSeccion = idSeccion;
    }

    /**
     * @return the idAsignatura
     */
    public Integer getIdAsignatura() {
        return idAsignatura;
    }

    /**
     * @param idAsignatura the idAsignatura to set
     */
    public void setIdAsignatura(Integer idAsignatura) {
        this.idAsignatura = idAsignatura;
    }

    /**
     * @return the idAnio
     */
    public Integer getIdAnio() {
        return idAnio;
    }

    /**
     * @param idAnio the idAnio to set
     */
    public void setIdAnio(Integer idAnio) {
        this.idAnio = idAnio;
    }

    /**
     * @return the idEmpleado
     */
    public Integer getIdEmpleado() {
        return idEmpleado;
    }

    /**
     * @param idEmpleado the idEmpleado to set
     */
    public void setIdEmpleado(Integer idEmpleado) {
        this.idEmpleado = idEmpleado;
    }

    /**
     * @return the cedulaMatriculaList
     */
    public List<CedMatriTemp> getCedulaMatriculaList() {
        return cedulaMatriculaList;
    }

    /**
     * @param cedulaMatriculaList the cedulaMatriculaList to set
     */
    public void setCedulaMatriculaList(List<CedMatriTemp> cedulaMatriculaList) {
        this.cedulaMatriculaList = cedulaMatriculaList;
    }
    
    public List<CedMatriTemp>cedulaMatriculaFilter(){
        log.info("Iniciando cedulaMatriculaBean....");
        List<CedulaMatricula>cedMatricula=null;
        String  strIdCiclo="1";
        String  strIdGrado="1"; 
        String strIdSeccion="1";
        String strIdArea="1";
        String strIdEmpleado="1";
        String strIdCronEval="1" ;
        String strIdAsignatura="1";
        String strIdAnio="1";
               
       
        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext externalContext = facesContext.getExternalContext();

            Map params = externalContext.getRequestParameterMap();
            strIdCiclo = ((String) params.get("idNota:idSelectCiclo"));
            strIdGrado = ((String) params.get("idNota:idSelectGrado"));
            strIdSeccion = ((String) params.get("idNota:idSelectSeccion"));
            strIdArea = ((String) params.get("idNota:idSelectArea"));
            strIdEmpleado = ((String) params.get("idNota:idSelectEmpleado"));
            strIdCronEval = ((String) params.get("idNota:idSelectCronEval"));
            strIdAsignatura = ((String) params.get("idNota:idSelectAsig"));
        } catch (Exception e) {
            System.out.println("Error al capturar parametro de busqueda de alumonos matriculados:" + e.getMessage());
        }
        System.out.println("strIdCiclo:" + strIdEmpleado);
        System.out.println("strIdGrado:" + strIdGrado);
        System.out.println("strIdSeccion:" + strIdSeccion);
        System.out.println("strIdArea:" + strIdArea);
        System.out.println("strIdEmpleado:" + strIdEmpleado);
        System.out.println("strIdCronEval:" + strIdCronEval);
        System.out.println("strIdAsignatura:" + strIdAsignatura);
           
        
        CedulaMatriculaDB cedulaMatriculaDB=new CedulaMatriculaDB();
        try {
            
          cedMatricula= cedulaMatriculaDB.getCedulaMatriculaFilter(strIdGrado, strIdSeccion,
                  strIdAsignatura, strIdAnio, strIdEmpleado);
          cedulaMatriculaList.clear();
          for(CedulaMatricula cd: cedMatricula){
              CedMatriTemp cedMatri=new CedMatriTemp();
              cedMatri.setCedulaMatricula(cd);
              getCedulaMatriculaList().add(cedMatri);
          }
            
            
        } catch (Exception e) {
            log.error("Error:al consultar tablaa cedula_Matricula"+e.getMessage());
            e.printStackTrace();
        }
       
        
        
        return cedulaMatriculaList;
        
    }

    /**
     * @return the currentCedMatIndex
     */
    public int getCurrentCedMatIndex() {
        return currentCedMatIndex;
    }

    /**
     * @param currentCedMatIndex the currentCedMatIndex to set
     */
    public void setCurrentCedMatIndex(int currentCedMatIndex) {
        this.currentCedMatIndex = currentCedMatIndex;
    }

    /**
     * @return the editCedMatricula
     */
    public CedMatriTemp getEditCedMatricula() {
        return editCedMatricula;
    }

    /**
     * @param editCedMatricula the editCedMatricula to set
     */
    public void setEditCedMatricula(CedMatriTemp editCedMatricula) {
        this.editCedMatricula = editCedMatricula;
    }

    /**
     * @return the page
     */
    public int getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * @return the clientRows
     */
    public int getClientRows() {
        return clientRows;
    }

    /**
     * @param clientRows the clientRows to set
     */
    public void setClientRows(int clientRows) {
        this.clientRows = clientRows;
    }
    
    
    public void agregarNota() {
        String result = "";
        Nota pro = new Nota();
        List<DetalleNota> dtListNota=new ArrayList<DetalleNota>();
        
        //Recorremos la lista de cedula matricula
        for(CedMatriTemp cd : cedulaMatriculaList ){
            System.out.println("cd:Nota1:"+cd.getDblNota1());
            DetalleNota dtNota1=new DetalleNota();
            dtNota1.setCalificacion(BigDecimal.valueOf(cd.getDblNota1()));
            dtNota1.setCapacidad(new Capacidad(1));
            dtNota1.setAlumno(cd.getCedulaMatricula().getAlumno());
            dtNota1.setCodCriterioEvaluacion(null);
            dtListNota.add(null);
        }

        System.out.println("Iniciando registro notas: ");
       
        //ProfesorDB proDB = new ProfesorDB();
        
        NotaDB notaDB=new NotaDB();
        
        setMensaje("Se realizó exitoamente la transacción");
        //if (result == 1) {
         //   mensaje = "* Se guardo correctamente";
        //} else {
        //    mensaje = "* No se guardo correctamente";
        //}

    }

    /**
     * @return the mensaje
     */
    public String getMensaje() {
        return mensaje;
    }

    /**
     * @param mensaje the mensaje to set
     */
    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }

}
